function _isBattery() {
	if ((window.navigator && window.navigator.battery) || window.navigator.webkitBattery || window.navigator.mozBattery || window.navigator.msBattery) {
		var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;
		return !battery.charging;
	} else if (window.navigator && window.navigator.getBattery) {
		return (async function () {
			const battery = await navigator.getBattery();
			if (battery.charging && battery.charging === true && battery.chargingTime === 0) return false;
			return true;
		})();
	} else return false;
}

function _copyToClipboard(text) {
	if (navigator.clipboard && navigator.clipboard.writeText) {
		return (async function () {
			return await navigator.clipboard.writeText(text);
		})();
	} else if (window.clipboardData && window.clipboardData.setData) {
		return window.clipboardData.setData("Text", text);
	} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
		const textarea = document.createElement("textarea");
		textarea.textContent = text;
		textarea.style.opacity = "0";
		textarea.style.position = "fixed";
		document.body.appendChild(textarea);
		textarea.select();
		try {
			return document.execCommand("copy");
		} catch (ex) {
			console.warn("Copy to clipboard failed.", ex);
			return false;
		} finally {
			document.body.removeChild(textarea);
		}
	}
}

run_task(function () {
	var isBat = _isBattery();
	if (isBat instanceof Promise) {
		isBat.then(function (v) {
			isBat = v;
		});
		window.isBattery = function () {
			run_task(function () {
				isBat = _isBattery().then(function (v) {
					isBat = v;
				});
			}, libWeb.TaskDispatcher.TaskPriority.BACKGROUND_TASK);
			return isBat;
		};
	} else {
		window.isBattery = _isBattery;
	}
}, libWeb.TaskDispatcher.TaskPriority.BLOCKING_TASK);
window.copyToClipboard = _copyToClipboard;
